<%@page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>

<%@page import="java.sql.Connection,
java.sql.PreparedStatement,
java.sql.ResultSet,
java.sql.SQLException,
java.sql.Statement,
java.util.ArrayList,
java.util.Date,
java.util.List"
%>
<%@page import= "edu.gdut.imis.goods.common.*"%>
<%@page import= "edu.gdut.imis.goods.entity.*"%>
<%@page import= "edu.gdut.imis.goods.dao.*"%>
<%@page import= "edu.gdut.imis.goods.business.*"%>
<%@page import= "edu.gdut.imis.goods.business.factory.EBOFactory"%>

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>购物车</title>
    <link rel="shortcut icon" href="favicon1.ico" />
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/common.css">
    <link rel="stylesheet" href="css/index.css">
    <style>
      .out{
           margin:30px 700px;
           height:220px;
           width:500px;
           border:2px solid black;
       }

      
       img{
           height:200px;
           width:200px;
           
       }
        .left{
         float:left;
       }
       .right{
           float:right;
           font-size:14px;
           font-weight:2;
           margin-right:50px;
       }
       .zhongbu{
           font-size:16px;
           float:left;
           margin:0px 50px;

       }
       .dibu{
           margin-left:550px;
       }
        .tijiao{
            color:while;
        }
    </style>
</head>
<body>

        <header class="w">
        <div class="logo">
            <h1><a href="index.html">天天淘商城</a></h1>
        </div>
        <form action="/Goods/find" method="POST">
            <input type="hidden" name="submitFlag" value="find">
            <div class="search">
                <input type="text" placeholder="电视机" name="fullName" >
                
                <button type="submit">搜索</button>
                <input type="text" name="id" id="search_id" style="display:none;">
                <!-- <input type="hidden"  name="id"> -->
                
            </div>
        </form>
        <div class="hotword">                        
            <a href="#" class="style_red">优惠购首发</a>
            <a href="#">亿元优惠</a>
            <a href="#">9.9元团购</a>
            <a href="#">美满99减30</a>
            <a href="#">办公用品</a>
            <a href="#">电脑</a>
            <a href="#">通信</a>
        </div>

        <a href="showCart.jsp" target='_blank'>
            <div class="shopping-car">
                我的购物车&nbsp;&gt;
                <i class="count">8888</i>
            </div>
        </a>
       
        
    </header>


   <form action="OrderServlet.jsp" method="post">
     

    <%
    response.setContentType("text/html;charset=utf-8");
    request.setCharacterEncoding("utf-8");
    

    
    float total = 0;
    int uid=0;
    GoodsModel gm = null;
    GoodsItem goodsitem = null;
    //商品id
    String id = request.getParameter("id");
    gm = EBOFactory.getGoodsEBO().findByID(id);
    
    
    goodsitem = new GoodsItem(gm,1);
    
    ShoppingCart cart = (ShoppingCart) session.getAttribute("cart");
    //进入购物车时要先判断用户是否为已登录状态
    User user = (User) session.getAttribute("user");

    if(user==null) {
        response.sendRedirect("login.html");
    }
    if(user!=null) {
    uid = user.getId();
    }
//  int uid = (int) session.getAttribute("uid");
//  out.print("<h3>"+user.getId()+"</h3>");
//  int uid = user.getId();
    if(cart!=null) {    
        if(!(gm==null)) {
        cart.add(goodsitem);
        //添加商品后遍历
        }
        ArrayList<GoodsItem>items = new ArrayList<GoodsItem>(cart.getItems());
        for(GoodsItem goodsItem:items) {
            GoodsModel goodm = goodsItem.getGoodsModel();
            String gid = goodm.getId();
            String Imgsrc = goodm.getImg_src();
            String description = goodm.getDescription();
            float price = goodm.getPrice();
            int quantity = goodsItem.getQuantity();     
            total = total+goodm.getPrice()*goodsItem.getQuantity();
            %>
            <div class="out">
                <input type="checkbox" name="productItem" value="<%=gid%>/<%=quantity%>">
                <div class="left">
                    <img alt="" src="upload/<%=Imgsrc%>">
                </div>
                <div class="right">
                    <p>描述:<%=description%></p>
                    <span>价格:<%=price%></span>
                    <p style="font-size: 30px;"><%=quantity%>件</p>
                    <a href="/Goods/addGoodsServlet?id=<%=gid%>" style="font-size: 40px;font-weight: 900;">+</a>
                    <a href="/Goods/removeGoodsServlet?id=<%=gid%>" style="font-size: 45px;font-weight: 900;">-</a>
                   
                </div>
            </div>      
    
            <%
        }
        
        
        session.setAttribute("cart", cart);
    
    }else {
        if(!(gm==null)) {
        ShoppingCart sc = new ShoppingCart();
        sc.add(goodsitem);
        
        //添加商品后遍历
        ArrayList<GoodsItem>items = new ArrayList<GoodsItem>(sc.getItems());
        
        for(GoodsItem goodsItem:items) {
            GoodsModel goodm = goodsItem.getGoodsModel();
            String gid = goodm.getId();
            String Imgsrc = goodm.getImg_src();
            String description = goodm.getDescription();
            float price = goodm.getPrice();
            int quantity = goodsItem.getQuantity();     
            total = total+goodm.getPrice()*goodsItem.getQuantity();
            %>
            <div class="out">
                <input type="checkbox" name="productItem" value="<%=gid%>/<%=quantity%>">
                <div class="left">
                    <img alt="" src="upload/<%=Imgsrc%>">
                </div>
                <div class="right">
                    <p><%=description%></p>
                    <span>价格:<%=price%></span>
                    <p style="font-size: 30px;"><%=quantity%>件</p>
                    <a href="/Goods/addGoodsServlet?id=<%=gid%>" style="font-size: 40px;font-weight: 900;">+</a>
                    <a href="/Goods/removeGoodsServlet?id=<%=gid%>" style="font-size: 45px;font-weight: 900;">-</a>
                </div>
            </div>      
    
            <%
            
        
            
            
        }
       
                
                    
        session.setAttribute("cart", sc);
        
        
        }else {
           out.print("null");
        }
        
    }
   
    
    
    %>
    <input type="text" name="id" value="<%=uid%>" style="display: none" >
    
    <ul class="dibu">
                    <li class="zhongbu tijiao"><input type="submit" value="提交订单" >
                    </li>
                    <li class="zhongbu"><h1><a href='index.html'>返回主页</a></h1>
                    </li>
                    <li class="zhongbu"><h1><a href='/Goods/ClearCartServlet'>清空购物车</a></h1>
                    </li>
                </ul>
</form>
    
    
    
    
</body>
</html>

